package com.company.thread;

import cn.hutool.core.thread.ThreadUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.LockSupport;

/**
 * @description: 线程不安全 的集合类
 * @author: ruoan
 * @date: 2020/12/8 16:32
 */
public class ConcurentCollDemo {

    public static void main(String[] args) {

        final Object obj = new Object();

        //List<String> li = new ArrayList<>(); //并发修改异常
        List<String> li = new CopyOnWriteArrayList<>(); // 解决办法之写时复制
        for(int i=0;i<80;i++){
            new Thread(() -> {
                li.add(UUID.randomUUID().toString().substring(0,8));
            },String.valueOf(i)).start();
        }

        while (Thread.activeCount() > 2) {
            Thread.yield();
        }

        System.out.println(li.size());
        System.out.println(li);




    }
}
